package com.changgou.search.controller;

import cn.hutool.core.util.ObjUtil;
import com.changgou.search.service.ESManagerService;
import com.changgou.search.service.SearchService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/sku_search")
public class SearchController {
    @Resource
    private ESManagerService esManagerService;
    @Resource
    private SearchService searchService;


    /**
     * 全文检索
     *
     */
    @GetMapping
    public Map<String, Object> search(@RequestParam Map<String, String> paramMap) throws Exception {
        //特殊符号处理
        this.handlerSearchMap(paramMap);
        return searchService.search(paramMap);
    }

    //对搜索入参带有特殊符号进行处理
    private void handlerSearchMap(Map<String, String> searchMap) {
        if (ObjUtil.isEmpty(searchMap)) {
            Set<Map.Entry<String, String>> entries =
                    searchMap.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                if (entry.getKey().startsWith("spec_")) {
                    searchMap.put(entry.getKey(), entry.getValue().replace("+", "%2B"));
                }
            }
        }
    }


}
